const express = require('express')
const statisticsRouter = express.Router()
const { body, validationResult } = require('express-validator')
const { raw } = require('body-parser')
const userModel = require("../modules/user_tab")
const tagModel = require("../modules/tag_tab")
const triendModel = require("../modules/triend_tab")
const commentModel = require("../modules/comment_tab")

statisticsRouter.get('/tagsNum',async(req,res,next) => {
    try {
        const where = {
          is_delete:0
        }
        const [users, tags, triends, comments] = await Promise.all([
          userModel.findAll({ raw: true, where: where }),
          tagModel.findAll({ raw: true, where: where }),
          triendModel.findAll({ raw: true, where: where }),
          commentModel.findAll({ raw: true, where: where })
        ]);
        res.json({
          code: 1,
          msg: '请求成功',
          data:{
            users:users.length,
            tags:tags.length,
            triends:triends.length,
            comments:comments.length
          }
        });
      } catch (err) {
        next(err);
      }
})
// 错误处理中间件
statisticsRouter.use((err, req, res, next) => {

  res.status(500).json({
    code: 0,
    msg: '服务器内部错误',
  });
});


module.exports = statisticsRouter